package principal;

import java.awt.EventQueue;
import java.awt.Frame;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JRootPane;
import javax.swing.border.EtchedBorder;

import java.awt.Color;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import com.linuxense.javadbf.DBFException;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Vector;
import java.awt.Cursor;

import javax.swing.ListSelectionModel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.SwingConstants;
public class Presentacion extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	public static JTable table;
	private JButton btnIngresar;
	private JButton btnModificar;
	private JButton btnEliminar;
	private JLabel title;
	private JPanel panel;
	
	public static void main(String[] args) {
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Presentacion frame = new Presentacion();
					frame.setUndecorated(true);
					frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
					//frame.setExtendedState(MAXIMIZED_BOTH);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	public static String base = "SOCIOS.DBF";
	
	int NumeroDecamposAmostrar = 8;
	public static void lectura(String ficheroOriginal, JTable tab1) {
		
		try {
            
           FileInputStream fileInput;
           fileInput = new FileInputStream(ficheroOriginal);

           DBFReader reader = new DBFReader(fileInput);
           ArrayList<String> campo = new ArrayList<String>();
           campo.add("SOCIO");
           campo.add("NOMBRE");
           campo.add("DOMICILIO");
            
            Object[] rowObjects;
            String filas ="";
    		DefaultTableModel modelo = new DefaultTableModel(null,campo.toArray());
    		DBFField field;
    		String Zzz;
    		 while ((rowObjects = reader.nextRecord()) != null) {
    		for (int e = 0; e < 2; e++) {
				
    			Zzz = campo.get(e);    		
          
            	
            	for (int i = 0; i < reader.getFieldCount(); i++) {
            		
                     field = reader.getField(i);
                   
                     if (Zzz.equals(field.getName())) {
                    	 //System.out.println(Zzz +" es igual a "+field.getName());
                    	 filas += rowObjects[i]+"#";
                   	
					}
                     }
            	System.out.println(filas);
            	modelo.addRow(filas.split("#"));
                
            }
           
    		}
            fileInput.close();
            
            tab1.setModel(modelo);
        
        } catch (DBFException | FileNotFoundException e) {
            e.getMessage();
        
        } catch (IOException e) {
            e.getMessage();
        
        } 
        
    }
	
	
	public Presentacion() {
				
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 452, 471);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		scrollPane.setAutoscrolls(true);
		
		title = new JLabel("");
		title.setHorizontalAlignment(SwingConstants.CENTER);
		title.setFont(new Font("Nueva Std", Font.ITALIC, 29));
		
		panel = new JPanel();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(22)
							.addComponent(title, GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE))
						.addComponent(panel, GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(title)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE)
					.addGap(18)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		
		btnIngresar = new JButton("Ingresar");
		btnIngresar.setHorizontalAlignment(SwingConstants.RIGHT);
		btnIngresar.setVerticalAlignment(SwingConstants.BOTTOM);
		panel.add(btnIngresar);
		
		btnModificar = new JButton("Modificar");
		btnModificar.setHorizontalAlignment(SwingConstants.RIGHT);
		btnModificar.setVerticalAlignment(SwingConstants.BOTTOM);
		panel.add(btnModificar);
		
		btnEliminar = new JButton("Eliminar");
		btnEliminar.setHorizontalAlignment(SwingConstants.RIGHT);
		btnEliminar.setVerticalAlignment(SwingConstants.BOTTOM);
		panel.add(btnEliminar);
		
		JButton btnFinalizar = new JButton("Finalizar...");
		btnFinalizar.setHorizontalAlignment(SwingConstants.RIGHT);
		btnFinalizar.setVerticalAlignment(SwingConstants.BOTTOM);
		panel.add(btnFinalizar);
		
		btnFinalizar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(DISPOSE_ON_CLOSE);
				
			}
		});
		btnIngresar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Frame frame2 = new Frame(); 
				frame2.setVisible(true);
			}
		});
	
	
		table = new JTable();
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		table.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		
		
		
		
				String cadena = base.substring(0, base.length()-4); 
		title.setText("Grilla de la tabla " +cadena);
		lectura("src/recursos/"+base, table);
		
	
		table.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, Color.BLACK, Color.ORANGE, Color.BLACK, Color.ORANGE));
		scrollPane.setViewportView(table);
		contentPane.setLayout(gl_contentPane);
		
	}
					
}
